package com.example.demo.controller.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhanpengguo
 * @date 2019-07-03 17:40
 */
@ControllerAdvice
public class WebExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest request, HttpServletResponse response, Exception e){
        e.printStackTrace();
        System.out.println("url:"+request.getRequestURL());
        System.out.println("uri:"+request.getRequestURI());
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception",e);
        mv.addObject("url",request.getRequestURL());
        mv.setViewName("error");
        return mv;
    }
}
